package org.njbdqn.mybuy.database;

import java.io.FileInputStream;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;



public class DbUtil {
	private static String DRIVER;
	private static String URL;
	private static String USERNAME;
	private static String PASSWORD;
	private static int MAX;
	private static int INIT;
	private static String QUERTSTR;
	
	static{
		try {
			readProperties();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private DbUtil(){}
	
	private static void readProperties()throws Exception{
		Properties p = new Properties();
		String path = URLDecoder.decode(DbUtil.class.getResource("/driver.properties").getPath(),"UTF-8");
		p.load(new FileInputStream(path));
		DRIVER = p.getProperty("driver");
		URL = p.getProperty("url");
		USERNAME = p.getProperty("username");
		PASSWORD = p.getProperty("password");
		MAX = Integer.parseInt(p.getProperty("max"));
		INIT = Integer.parseInt(p.getProperty("init"));
		QUERTSTR = p.getProperty("queryStr");
	}
	public synchronized static Connection getConnection(){
		return Pool.buildConnection().getConn();
	}
	
	public static void destory(Connection conn){
		Pool.destory(conn);
	}
	
	private static class Pool{
		private static List<ConnectionEntry> ces = new ArrayList<ConnectionEntry>();
		
		public static ConnectionEntry buildConnection(){
			ConnectionEntry ce =null;
			if (ces.isEmpty()) {
				for (int i = 0; i < INIT; i++) {
					ces.add(createConnection());
				}
				ce = ces.get(0);
			}else{
				for(ConnectionEntry c:ces){
					if (c.isFree()) {
						ce=c;
						break;
					}
				}
				if (ce==null) {
					if (ces.size()<MAX) {
						ce = createConnection();
						ces.add(ce);
					} else {
						try {
							Thread.sleep(1000);
							buildConnection();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			ce.setFree(false);
			return ce;
		}
		private static ConnectionEntry createConnection(){
			ConnectionEntry ce = new ConnectionEntry();
			try {
				Class.forName(DRIVER);
				Connection con = DriverManager.getConnection(URL,USERNAME,PASSWORD);
				ce.setConn(con);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return ce;
		}
		
		public static void destory(Connection conn){
			for(ConnectionEntry ce:ces){
				if (conn == ce.getConn()) {
					ce.setFree(true);
					try {
						PreparedStatement pstat = conn.prepareStatement(QUERTSTR);
						pstat.executeQuery();
					} catch (SQLException e) {
						try {
							ces.remove(ce);
							conn.close();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				}
			}
		}
	}
//	public static void main(String[] args) {
//		System.out.println(getConnection());
//	}
}
